<?php

namespace App\Sidong\Controllers;

use App\Models\Coach;
use App\Models\GroupCourse;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GroupCourseController extends AdminController
{
    protected $title = '团课管理';

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '团课列表'])
            ->body($this->grid());
    }

    public function create(Content $content)
    {
        return $content
            ->title('新增团课')
            ->breadcrumb(['text' => '团课列表', 'url' => 'group-courses'], ['text' => '新增团课'])
            ->body($this->form());
    }

    public function edit($id, Content $content)
    {
        return $content
            ->title('编辑团课')
            ->breadcrumb(['text' => '团课列表', 'url' => 'group-courses'], ['text' => '编辑团课'])
            ->body($this->form()->edit($id));
    }

    protected function grid()
    {
        $grid = new Grid(new GroupCourse);
        $grid->disableBatchActions();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });
        $grid->model()->orderByDesc('sort')
            ->orderByDesc('created_at')
            ->orderByDesc('is_enable');
        $grid->column('id', __('ID'))->sortable();
        $grid->column('title', '名称');
        $grid->column('cover_url', '封面图')->lightbox(['width' => 35, 'height' => 35]);
        $grid->column('coach.realname', '教练姓名');
        $grid->column('price', '价格')->display(function ($price) {
            return $price > 0 ? "¥{$price}" : '免费';
        });
        $grid->column('people_number', '可上课人数');
        $grid->column('start_people_number', '开课人数');
        $grid->column('schedule_type', '排课类型')->display(function ($schedule_type) {
            return $schedule_type == 1 ? '<span class="label label-success">按周排课</span>' : '<span class="label label-danger">按日期排课</span>';
        });
        $grid->column('course_time', '上课时间')->display(function () {
            $week_name = GroupCourse::WEEKS[$this->week];
            $text = $this->schedule_type == GroupCourse::SCHEDULE_TYPE_WEEK ? "每{$week_name} " : "{$this->date} ";
            return $text . $this->course_start_time . '~' . $this->course_end_time;
        });
        $grid->column('tags', '标签')->display(function($tags) {
            return implode(',', $tags);
        });
        $grid->column('deadline', '截止报名时间')->display(function($deadline) {
            return GroupCourse::DEADLINE[$deadline];
        });
        $grid->column('total_buy_count', '累计报名')->display(function($total_buy_count) {
            return $total_buy_count . '次';
        });
        return $grid;
    }

    protected function form()
    {
        $admin = \Admin::user();
        $form = new Form(new GroupCourse);
        $form->hidden('tenant_id')->default($admin->tenant_id);
        $form->text('title', '标题')
            ->placeholder('请输入团课标题，如：急速动感单车')
            ->rules('required');
        $form->select('coach_id', '团课教练')
            ->placeholder('请选择团课教练')
            ->options(Coach::getSelectOptions(2))
            ->help('请选择团课教练')
            ->rules('required');
        $form->image('cover_url', '封面图')
            ->help('显示在小程序团课列表的封面图片，建议尺寸800*800')
            ->rules('required')
            ->crop(800, 800);
        $form->currency('price', '价格')
            ->placeholder('请输入团课价格')
            ->help('团课价格，0表示免费。如需输入小数请在数字后继续输入点号即可')
            ->width('200px')
            ->symbol('￥')
            ->default(0)
            ->rules('required');
        $form->number('people_number', '可上课人数')
            ->placeholder('请输入可上课人数')
            ->default(1)
            ->min(1)
            ->help('输入能接纳的上课的最大人数，报名超过此人数后就无法报名了')
            ->rules('required');
        $form->number('start_people_number', '开课人数')
            ->placeholder('请输入开课人数')
            ->default(3)
            ->min(1)
            ->help('开课人数表示满足开课的最低人数才能开始上课')
            ->rules('required');
        $form->radio('schedule_type', '排课类型')
            ->addElementClass('schedule-type')
            ->options([1 => '按周排课', 2=> '按日期排课'])
            ->default(GroupCourse::SCHEDULE_TYPE_WEEK)
            ->help('说明：选择按周排课即表示课程按照每周进行排课，小程序课程列表会自动计算每周的排课表。选择按日期排课此课程只有在选定的日期才能展示出来');
        $form->select('week', '按周上课')
            ->placeholder('请选择周')
            ->options(GroupCourse::getWeeksSelectOptions())
            ->default(1)
            ->help('选择此课程在周几上课')
            ->addElementClass(['schedule-type-week']);
        $form->date('date', '按日期上课')
            ->placeholder('选择日期')
            ->width('200px')
            ->help('请选择上课日期')
            ->addElementClass(['schedule-type-date']);
        $form->timeRange('course_start_time', 'course_end_time', '上课时间')
            ->help('请填写上课时间')
            ->width('200px')
            ->rules('required');
        $form->select('deadline', '截止报名时间')
            ->placeholder('请选择截止报名时间')
            ->options(GroupCourse::getDeadlineSelectOptions())
            ->help('请选择截止报名时间，超过此时间将无法报名参加团课')
            ->rules('required');
        $form->multipleSelect('tags', '课程标签')
            ->options(Coach::getTagsSelectOptions())
            ->placeholder('请选择课程标签，可多选')
            ->help('请选择课程标签，可多选');
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $form->switch('is_enable', '是否启用')
            ->default(1)
            ->help('注意：如果选择禁用则课程不会出现在小程序上的团课列表')
            ->states($states);
        $form->number('sort', '排序')
            ->default(1)
            ->min(1)
            ->help('输入排序数字，值越大越靠前');
        $form->textarea('intro', '课程简介')
            ->placeholder('课程的详细介绍');
        $form->textarea('notes', '课程须知')
            ->placeholder('请输入上课的一些须知，主要告知用户一些注意事项。如：需要自备瑜伽垫或者其他注意事项等');
        $script = <<<SCRIPT
$('.schedule-type-date').parents('.form-group').hide();
$('.schedule-type').on('ifChecked', function(event){
  var value = $(this).val();
  if (value === '1') {
    $('.schedule-type-week').parents('.form-group').show();
    $('.schedule-type-date').parents('.form-group').hide();
  } else if (value === '2') {
    $('.schedule-type-week').parents('.form-group').hide();
    $('.schedule-type-date').parents('.form-group').show();
  }
});
SCRIPT;

        \Admin::script($script);

        return $form;
    }
}
